//
//  ClockView.swift
//  FromWatchToCircle
//
//  Created by 张海峰 on 2018/5/31.
//  Copyright © 2018年 张海峰. All rights reserved.
//
/*写该demo的目的：主要是想从时钟开始，一步步窥见项目中和圆有关的东西，从而学习巩固，加深理解。
 (项目不定时更新，所以最好从下面地址获取最新demo),感觉我这个demo对你有启发或者帮助，欢迎fork 感谢star
 https://github.com/FighterLightning/FromWatchToCircle.git
 */
/*时钟视图*/

import UIKit

class ClockView: UIView,UIGestureRecognizerDelegate {
    //表盘宽高
    let clockWH: CGFloat = 200;
    var hourLayer: CALayer = CALayer()
    var minuteLayer: CALayer = CALayer()
    var secondLayer: CALayer = CALayer()
    //初始化视图
    func initClockView() -> UIView{
        self.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
        //背景区域的颜色和透明度
        self.backgroundColor = UIColor.init(red: 225 , green: 225, blue: 225, alpha: 0.8)
        //添加表视图
        self.addClockImageViewAndLayer()
        let tap: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapClick))
        tap.delegate = self
        self.addGestureRecognizer(tap)
        return self
    }
    func addClockImageViewAndLayer(){
        //1.添加表盘
        let clockView: UIImageView = UIImageView.init(frame: CGRect.init(x: (ScreenWidth - clockWH)/2 , y: (ScreenHeight - clockWH)/2, width: clockWH, height: clockWH))
        clockView.image = UIImage.init(named: "clock")
        self.addSubview(clockView)
        //添加中心点
        let centerLayer: CALayer = CALayer.init()
        centerLayer.bounds = CGRect.init(x: 0, y: 0, width: 10, height: 10)
        centerLayer.position = CGPoint.init(x: clockWH/2, y: clockWH/2)
        centerLayer.cornerRadius = 5
        centerLayer.backgroundColor = UIColor.black.cgColor
        clockView.layer.addSublayer(centerLayer)
        //添加时针
        hourLayer = CALayer.init()
        hourLayer.bounds = CGRect.init(x: 0, y: 0, width: 5, height: 60)
        hourLayer.cornerRadius = 2.5
        hourLayer.anchorPoint = CGPoint.init(x: 0.5, y: 0.95)
        hourLayer.position = CGPoint.init(x: clockWH/2, y: clockWH/2)
        hourLayer.backgroundColor = UIColor.black.cgColor
        clockView.layer.addSublayer(hourLayer)
        //添加分针
        minuteLayer = CALayer.init()
        minuteLayer.bounds = CGRect.init(x: 0, y: 0, width: 4, height: 70)
        minuteLayer.cornerRadius = 2
        minuteLayer.anchorPoint = CGPoint.init(x: 0.5, y: 0.9)
        minuteLayer.position = CGPoint.init(x: clockWH/2, y: clockWH/2)
        minuteLayer.backgroundColor = UIColor.black.cgColor
        clockView.layer.addSublayer(minuteLayer)
        //添加秒针
        secondLayer = CALayer.init()
        secondLayer.bounds = CGRect.init(x: 0, y: 0, width: 2, height: 100)
        secondLayer.cornerRadius = 2
        secondLayer.anchorPoint = CGPoint.init(x: 0.5, y: 0.8)
        secondLayer.position = CGPoint.init(x: clockWH/2, y: clockWH/2)
        secondLayer.backgroundColor = UIColor.red.cgColor
        clockView.layer.addSublayer(secondLayer)
        //定时器 每秒更新一次数据
        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
        self.update()
    }
    @objc func update(){
        //日历
        let calendar:Calendar = Calendar.current
        //获取日历组件
        let componentsSet = Set<Calendar.Component>([.hour, .minute, .second])
        let components = calendar.dateComponents(componentsSet, from: Date.init())
        //获取当前秒
        let second:CGFloat = CGFloat(components.second!)
        //获取当前分钟
        let minute:CGFloat = CGFloat(components.minute!)
        //获取当前小时
        let hour:CGFloat = CGFloat(components.hour!)
        //每秒钟 秒针走的弧度
        let perSecondAngle: CGFloat = CGFloat(Double.pi*2/60)
        //每分钟 分针走的弧度
        let perMinuteAngle: CGFloat = CGFloat(Double.pi*2/60)
        //每秒钟 分针走的弧度
        let perSecondMinuteAngle: CGFloat = perMinuteAngle/60
        //每小时 时针走的弧度
        let perHourAngle: CGFloat = .pi*2/12
        //每分钟 时针走的弧度
        let perMinuteHourAngle: CGFloat = perHourAngle/60
        //当前时间 秒针走过的弧度
        let secondAngle: CGFloat = second * perSecondAngle
        //当前时间 分针走过的弧度 = 当前分钟 * 每分钟分针走的弧度 + 当前秒 * 每秒钟分针走的弧度
        let minuteAngle: CGFloat = minute * perMinuteAngle + second * perSecondMinuteAngle
        //当前时间 时针走过的弧度 = 当前小时 * 每小时时针走的弧度 + 当前分钟 * 每分钟时针走的弧度
        let hourAngle: CGFloat = hour * perHourAngle + minute * perMinuteHourAngle
        //秒针旋转到 当前弧度
        secondLayer.transform = CATransform3DMakeRotation(secondAngle, 0, 0, 1)
        //分针旋转到 当前弧度
        minuteLayer.transform = CATransform3DMakeRotation(minuteAngle, 0, 0, 1)
        //时针旋转到 当前弧度
        hourLayer.transform = CATransform3DMakeRotation(hourAngle, 0, 0, 1)
    }
    @objc func tapClick(){
       self.removeFromSuperview()
    }
}

